#include "ParametersBean.h"
#include "Messaging.h"

using namespace Networking::Messaging;

PlayerIDBean::PlayerIDBean(int id) :
    m_id(id)
{}

MessageParameters PlayerIDBean::ToList() const
{
    return MessageParameters(1, QString::number(m_id));
}

MessageBean::MessageBean(const QString& msg) :
    m_message(msg)
{}

MessageParameters MessageBean::ToList() const
{
    return MessageParameters(1, m_message);
}


//TurnRequestBean::TurnRequestBean(Tank::TurnDirectionType type) :
//    m_action(type)
//{}

//MessageParameters RequestTurnBean::ToList() const
//{
//    return MessageParameters(1, TurnDirections[m_action]);
//}

PlayerlistBean::PlayerlistBean(const Playerlist& playerlist) :
    m_players(playerlist)
{}

MessageParameters PlayerlistBean::ToList() const
{
    MessageParameters res;
    Player player;
    foreach(player, m_players)
    {
        res.push_back(QString::number(player.Id()));
        res.push_back(player.Nickname());
    }
    return res;
}

SnapshotBean::SnapshotBean(const Tanks& tanks, const Bullets& bullets) :
    m_snapshotTanks(tanks), m_snapshotBullets(bullets)
{}

MessageParameters SnapshotBean::ToList() const
{
    MessageParameters res;
    res.push_back(QString::number(m_snapshotTanks.size())); //tanks number

    TankPtr tankPtr;
    foreach(tankPtr, m_snapshotTanks)
    {
        res.push_back(QString::number(tankPtr->Id())); //tank id
        res.push_back(QString::number(tankPtr->Health())); //health lvl
        res.push_back(QString::number(tankPtr->CurrentPosition().x)); // pos x
        res.push_back(QString::number(tankPtr->CurrentPosition().y)); // pos y

        Tank::ActionType act = tankPtr->CurrentAction();
        QString actType = ActionTypes[act];
        QString direction = Directions[tankPtr->Direction()];

        res.push_back(actType); // action
        res.push_back(direction); // direction
        res.push_back(QString::number(tankPtr->Progress())); // progress
    }

    BulletPtr bulletPtr;
    foreach(bulletPtr, m_snapshotBullets)
    {
        //res.push_back(QString::number(bulletPtr->Id())); //bullet id
        res.push_back(QString::number(bulletPtr->CurrentPosition().x)); // pos x
        res.push_back(QString::number(bulletPtr->CurrentPosition().y)); // pos y

        QString direction = Directions[bulletPtr->Direction()];

        res.push_back(direction); // direction
        res.push_back(QString::number(bulletPtr->Progress())); // progress
    }
    return res;
}

MapBean::MapBean(QSharedPointer<Map> map, const GameSettings& sets) :
    m_map(map), m_settings(sets)
{}

MessageParameters MapBean::ToList() const
{
    MessageParameters res;
    res.push_back(QString::number(m_map->Width()));

    Cell* cell;
    foreach(cell, m_map->MapData())
    {
        res.push_back(QString(cell->Type()));
    }
    res.push_back(QString::number(m_settings.Health()));
    res.push_back(QString::number(m_settings.MoveSpeed()));
    res.push_back(QString::number(m_settings.BulletSpeed()));
    res.push_back(QString::number(m_settings.Firerate()));

    return res;
}

